window.onload=function(){
    var totalPages;
    function addfun(currentPage,resourceType,keywords){
        $.ajax({
            url:"http://47.112.110.215:8989/user/findUserList",
            type:"get",
            headers:{
                "Authorization": 'Bearer ' + localStorage.getItem("token"),
            },
            data:{
                page:currentPage,
                type:resourceType,
                keywords:keywords,
            },
               
            success: function(result) {
            console.log(result);
            
            console.log(111);
             var htmlString='';
             var userListDiv = document.getElementById("content");
             var contentList=result.data.records;
             totalPages=result.data.totalPages;
             for(var i=0;i<contentList.length;i++){
                var user=contentList[i];
                htmlString+='<tr class="data">';
                htmlString+='<td>'+user.id+'</td>';
                htmlString+='<td>'+user.username+'</td>';
                htmlString+='<td>'+user.email+'</td>';
                if(user.phone==null){
                    user.phone="暂无";
                 }
                htmlString+='<td>'+user.phone+'</td>';
                htmlString+='<td>'+user.userType+'</td>';
                //将原来的单引号改为反引号（`），这样，${user.id}就能正确地被用户ID替换。
                htmlString+='<td>'+`<button class="delete" value="${user.id}">删除</button>`+'</td>';
                htmlString+='<td>'+'<input type="button" class="Revise" value="配置">'+'</td>';
                htmlString+='</tr>';
                
             }
             userListDiv.innerHTML=htmlString;
           
           },
        error: function(xhr, status, error) {
            console.log(error);
          },
        })
    }
    
    addfun();

var login=document.getElementsByClassName("login")[0];
login.onclick=function(){
    location.href="denglu.html";
}
//添加功能
    var add=document.getElementById("add");
    console.log(add);
    add.onclick=function(){
        // alert(111);
        $('#fill').show();
        $('#myform').show();
       document.getElementById("addd").onclick=function(){
        var password=document.getElementsByClassName("password")[0].value;
        var username=document.getElementsByClassName("username")[0].value;
        var email=document.getElementsByClassName("email")[0].value;
        var phone=document.getElementsByClassName("phone")[0].value;
        var userType=document.getElementsByClassName("userType")[0].value;
        var json={
            "password": password,
            "email": email,
            "username":username,
            "phone":phone,
            "userType": userType
        }
        var jsonStr=JSON.stringify(json);
        console.log(jsonStr);
        $.ajax({
            url:"http://47.112.110.215:8989/user/addUser",
            type:"post",
            contentType: "application/json",
            data:jsonStr,
            headers:{
                "Authorization": 'Bearer ' + localStorage.getItem("token"),
            },
            success: function(result) {
                console.log("添加成功");
                addfun();
                $('#fill').hide();
                $('#myform').hide();

            },
            error: function(xhr, status, error) {
                // alert("添加失败");
                console.log(error);
                console.log(xhr.responseText);
              },
        });
       
       }
    }
    document.getElementById("close").onclick=function(){
        $('#fill').hide();
        $('#myform').hide();
    }



//删除功能
    document.addEventListener('click',function(event){
        if(event.target.matches('.delete')){
            var deleid=Number(event.target.value);
            alert("确定删除吗?");
            $.ajax({
                   url:"http://47.112.110.215:8989/user/delete?ids="+deleid,
                   type:'DELETE',
                   headers:{
                    "Authorization": 'Bearer ' + localStorage.getItem("token"),
                   },
                // data:{
                //     ids:deleid,
                // },
                    success: function(result) {
                        console.log("删除成功");
                        $(event.target).closest('tr').remove();
                        addfun();
                     },
                    error: function(xhr, status, error) {
                        alert("删除失败");
                        console.log(error);
                        console.log(xhr.responseText);
                    }
            });
        }
    })
            
      
                   
           
    //分页功能
    const pagination={
        currentPage:1,//当前页码
      }
    console.log('Current Page:', pagination.currentPage);
    var preBtn=document.getElementById("preBtn");
    var nextBtn=document.getElementById("nextBtn");
    
    preBtn.onclick=function(){
       if(pagination.currentPage>1){
            pagination.currentPage--;
            addfun(pagination.currentPage);
     }
    };
    nextBtn.onclick=function(){
        if(pagination.currentPage<totalPages){
            console.log("点击成功");
           pagination.currentPage++;
           addfun(pagination.currentPage);
        }
    };




//修改功能
  document.addEventListener('click',function(event){
      if(event.target.matches('input.Revise')){
        //如果你在按钮上添加了点击事件监听器，可以使用event.target来获取该按钮，
        //然后使用.closest()方法来找到最近的<tr>祖先元素。
       var row=event.target.closest('tr');
       console.log(row);
       // 遍历所有子元素(td或th)
       Array.from(row.children).forEach(function(cell) {
        console.log(cell);  // 输出每个 td 或 th 元素
    });
        document.getElementsByClassName("id")[0].value = row.children[0].innerHTML;
        document.getElementsByClassName("username")[1].value= row.children[1].innerHTML;
        document.getElementsByClassName("email")[1].value = row.children[2].innerHTML;
        document.getElementsByClassName("phone")[1].value= row.children[3].innerHTML;
        document.getElementsByClassName("userType")[1].value = row.children[4].innerHTML;
        
          //处理点击事件
          $('#revise-fill').show();
          $('#revise').show();

          document.getElementById("confirm-revise").onclick=function(){
            var id=Number(document.getElementsByClassName("id")[0].value);
            var username=document.getElementsByClassName("username")[1].value;
            var email=document.getElementsByClassName("email")[1].value;
            var phone=document.getElementsByClassName("phone")[1].value;
            var userType=document.getElementsByClassName("userType")[1].value;
            var json={
                "id":id,
                "email": email,
                "username":username,
                "phone":phone,
                "userType": userType
            }
            var jsonStr=JSON.stringify(json);
            console.log(jsonStr);
            $.ajax({
                url:"http://47.112.110.215:8989/user/update",
                type:"put",
                contentType: "application/json",
                data:jsonStr,
                headers:{
                    "Authorization": 'Bearer ' + localStorage.getItem("token"),
                },
                success: function(result) {
                    console.log("修改成功");
                    addfun();
                    $('#revise-fill').hide();
                    $('#revise').hide();
    
                },
                error: function(xhr, status, error) {
                    console.log("修改失败");
                    console.log(error);
                    console.log(xhr.responseText);
                  },
            });
           
           }
      }
  })
  document.getElementById("close-revise").onclick=function(){
    $('#revise-fill').hide();
    $('#revise').hide();
  }

  //查询功能
 document.getElementById("query-button").addEventListener('click',function(){
    var resourceType=document.getElementById("resourceType").value;
    var keywords=document.getElementById("searchKeywords").value;
    addfun(1,resourceType,keywords);
 });
 document.getElementById("reset").addEventListener('click',function(){
    addfun(1);
    document.getElementById("resourceType").value='';
    document.getElementById("searchKeywords").value='';
 })
   
}
    
    
